<?php

/**
 * artikel actions.
 *
 * @package    pendidikan indonesia
 * @subpackage artikel
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class artikelActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->artikels = Doctrine::getTable('artikel')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    //$this->form = new artikelForm();
    $artikelForm = new userArtikelForm();
    $artikelForm->setIdPenulis($this->getUser()->getAttribute('app_user_id'));
    $this->form = $artikelForm;
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new userArtikelForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($artikel = Doctrine::getTable('artikel')->find(array($request->getParameter('id'))), sprintf('Object artikel does not exist (%s).', $request->getParameter('id')));
    //$this->form = new artikelForm($artikel);

    $id_penulis = $this->getUser()->getAttribute('app_user_id');
    
    $artikelForm = new userArtikelForm($artikel);
    
    $this->form = $artikelForm;
    
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($artikel = Doctrine::getTable('artikel')->find(array($request->getParameter('id'))), sprintf('Object artikel does not exist (%s).', $request->getParameter('id')));
    $this->form = new userArtikelForm($artikel);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($artikel = Doctrine::getTable('artikel')->find(array($request->getParameter('id'))), sprintf('Object artikel does not exist (%s).', $request->getParameter('id')));
    $artikel->delete();

    $this->redirect('artikel/index');
  }

  public function executeList(sfWebRequest $request) {
      $id_penulis = $this->getUser()->getAttribute('app_user_id');
      $this->artikel = Doctrine_Core::getTable('artikel')->getArtikelByUser($id_penulis);
      
  }

  public function executeShow(sfWebRequest $request) {
      $this->forward404Unless($artikel = Doctrine_Core::getTable('artikel')->getArtikelById($request->getParameter('id')));

      $this->artikel = $artikel;
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $artikel = $form->save();

      //$this->redirect('artikel/edit?id='.$artikel->getId());
      $this->redirect('artikel/list');
    }
  }
}
